package com.sram.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sram.entity.User;

public class UserUploadAction extends ActionSupport{
	private User u;
	private File headImg;//临时文件
	private String headImgContentType;//文件类型
	private String headImgFileName;//文件名称

	public File getHeadImg() {
		return headImg;
	}

	public void setHeadImg(File headImg) {
		this.headImg = headImg;
	}

	public String getHeadImgContentType() {
		return headImgContentType;
	}

	public void setHeadImgContentType(String headImgContentType) {
		this.headImgContentType = headImgContentType;
	}

	public String getHeadImgFileName() {
		return headImgFileName;
	}

	public void setHeadImgFileName(String headImgFileName) {
		this.headImgFileName = headImgFileName;
	}

	public User getU() {
		return u;
	}

	public void setU(User u) {
		this.u = u;
	}

	public String execute(){
//		try {
//			Thread.sleep(5000);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
		System.out.println("执行了execute方法：");
		System.out.println(u);
		System.out.println("----------------------------------");
		System.out.println(headImg.getAbsolutePath());
		System.out.println(headImgContentType);
		System.out.println(headImgFileName);
		String realpath = ServletActionContext.getServletContext().getRealPath("/images");
		File file = new File(realpath);
		if(!file.exists()) file.mkdirs();
		try {
			FileUtils.copyFile(headImg, new File(file, headImgFileName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
}
